package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.reggie.bean.Emp;
import com.zrrd.reggie.service.EmpService;
import com.zrrd.reggie.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class EmpController {
    @Autowired
    private EmpService empService;
    @PostMapping("/login")
    public R<Emp> login(@RequestBody Emp emp, HttpServletRequest request){
        String username=emp.getUsername();
        String password = emp.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        QueryWrapper<Emp> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username);
        Emp e=empService.getOne(wrapper);
        if(e==null){
            return R.error("登陆失败,用户不存在");
        }
        if(!password.equals(e.getPassword())){
            return R.error("登录失败,密码错误");
        }
        if (e.getStatus()==0){
            return R.error("登录失败，员工被封禁");
        }
        request.getSession().setAttribute("empId",e.getId());
        return R.sucess(e);
    }
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("empId");
        return R.sucess("退出成功");
    }
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        QueryWrapper<Emp> wrapper=new QueryWrapper<>();
        if(name!=null&&!name.equals("")){
            wrapper.like("name",name);
        }
        Page<Emp> pageInfo=new Page<>(page,pageSize);
        empService.page(pageInfo,wrapper);
        return R.sucess(pageInfo);
    }
    @PostMapping
    public R<String> addEmp(@RequestBody Emp emp,HttpServletRequest request){
        //设置密码 MD5加密
        emp.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        Long empId = (Long)request.getSession().getAttribute("empId");
        emp.setCreateUser(empId);
        emp.setUpdateUser(empId);
        boolean save=empService.save(emp);
        if(save){
            return R.sucess("添加成功");
        }else {
            return R.error("添加失败");
        }
    }
    //根据id查询员工信息
    @GetMapping("/{id}")
    public R<Emp> getById(@PathVariable("id") Long id){
        return R.sucess(empService.getById(id));
    }

    //修改员工
    @PutMapping
    public R<String> updateEmp(@RequestBody Emp emp,HttpServletRequest request){
        //设置更新时间
        emp.setUpdateTime(LocalDateTime.now());
        //设置更新人的ID
        Long empId = (Long) request.getSession().getAttribute("empId");
        emp.setUpdateUser(empId);
        //执行修改
        boolean update = empService.updateById(emp);
        if(update){
            return R.sucess("修改成功");
        }else{
            return R.error("修改失败");
        }
    }
    /*
    @GetMapping("/{id}")
    public R<Emp> getById(@PathVariable("id") Long id){
        return R.sucess(empService.getById(id));
    }
    @PutMapping
    public R<String> updateEmp(@RequestBody Emp emp,HttpServletRequest request){
        emp.setUpdateTime(LocalDateTime.now());
        Long empId = (Long)request.getSession().getAttribute("empId");
        emp.setUpdateUser(empId);
        //这错了
        boolean update=empService.save(emp);
        if(update){
            return R.sucess("修改成功");
        }else {
            return R.error("修改失败");
        }
    }
     */
}
